home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Magazine / C_Tutorial / Part-12 / dt0 / main.c < prev    next >
C/C++ Source or Header  |  1998-05-04  |  7KB  |  283 lines

  1. #include<dos/dos.h>
  2. #include<dos/rdargs.h>
  3. #include<exec/libraries.h>
  4. #include<workbench/startup.h>
  5. #include<workbench/workbench.h>
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. #include<clib/dos_protos.h>
  11. #include<clib/exec_protos.h>
  12. #include<clib/icon_protos.h>
  13.  
  14. #include "main.h"
  15. #include "fractal.h"
  16. #include "gui.h"
  17. #include "idcmp.h"
  18. #include "loadsave.h"
  19. #include "arexx.h"
  20.  
  21. /* The library base global variables */
  22. /* (The different style of opening libraries requires these to be initialised to NULL) */
  23. struct Library* GfxBase = NULL;
  24. struct Library* IntuitionBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26. struct Library* AslBase = NULL;
  27. struct Library* DosBase = NULL;
  28. struct Library* IFFBase = NULL;
  29. struct Library* RexxSysBase = NULL;
  30. struct Library* IconBase = NULL;
  31. struct Library* DataTypesBase = NULL;
  32.  
  33. /* Need to give prototypes for our functions */
  34. static int  createAll(struct WBStartup*, char*);
  35. static void freeAll(void);
  36. static int  openLibs(void);
  37. static void closeLibs(void);
  38.  
  39. /* The alternative "main()" for Workbench startup */
  40. void wbmain(struct WBStartup*);
  41.  
  42. /* The shared starting point of our program */
  43. static void realmain(struct WBStartup*, char*);
  44.  
  45. #define TT_DEPTH       "DEPTH"
  46. #define TT_PORTNAME    "PORTNAME"
  47.  
  48. #define ARGS_TEMPLATE  "FILE," TT_DEPTH "/N," TT_PORTNAME
  49.  
  50. enum ARGS { ARG_FILENAME, ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  51.  
  52. #define DEFAULT_PORTNAME "HELLOPAINTER"
  53. #define DEFAULT_DEPTH    (4)
  54.  
  55. /* A place to store our program name for the default tool */
  56. static char progname[MAXFILENAME];
  57.  
  58. char* progName()
  59. {
  60.     return progname;
  61. }
  62.  
  63. static void setProgName(struct WBStartup* wbmsg, char* prog)
  64. {
  65.     BPTR dir;
  66.   char* file;
  67.   if(wbmsg)
  68.     {
  69.         dir = wbmsg->sm_ArgList[0].wa_Lock;
  70.         file = wbmsg->sm_ArgList[0].wa_Name;
  71.     }
  72.     else
  73.     {
  74.         dir = Lock("PROGDIR:", ACCESS_READ);
  75.         file = prog;
  76.     }
  77.     *progname = '\0';
  78.     NameFromLock(dir, progname, MAXFILENAME);
  79.     AddPart(progname, file, MAXFILENAME);
  80.   if(prog && dir)
  81.         UnLock(dir);
  82. }
  83.  
  84. /* The CLI starting point for StormC, but the general start for SAS/C */
  85. void main(int argc, char** argv)
  86. {
  87.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  88.   if(argc == 0)
  89.         wbmain((struct WBStartup*)argv);
  90.     else
  91.         realmain(NULL, argv[0]);
  92. }
  93.  
  94. /* The WB starting point for StormC */
  95. void wbmain(struct WBStartup* wbmsg)
  96. {
  97.     /* WB-specific startup could go here */
  98.     realmain(wbmsg, NULL);
  99. }
  100.  
  101. /* The start of the program */
  102. static void realmain(struct WBStartup* wbmsg, char* prog)
  103. {
  104.     if(createAll(wbmsg, prog))
  105.         handleIDCMP();
  106.     freeAll();
  107. }
  108.  
  109. static struct RDArgs* rdargs = NULL;
  110. static struct DiskObject* dobj = NULL;
  111.  
  112. static int createAll(struct WBStartup* wbmsg, char* prog)
  113. {
  114.     int success = FALSE;
  115.   if(openLibs())
  116.     {
  117.         /* We'll only set portname if successful */
  118.         char* portname = NULL;
  119.         UBYTE depth;
  120.         char* filename = NULL;
  121.         BPTR currdir = NULL;
  122.         setProgName(wbmsg, prog);
  123.         initSemaphores();
  124.         if(wbmsg)
  125.         {
  126.             /* WB bit, use Tool Types */
  127.             currdir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
  128.             if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
  129.             {
  130.                 UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
  131.                 char* depthptr = FindToolType(tt, TT_DEPTH);
  132.                 portname = FindToolType(tt, TT_PORTNAME);
  133.                 /* Use the default if a Tool Type was not specified */
  134.                 if(portname == NULL)
  135.                     portname = DEFAULT_PORTNAME;
  136.                 if(depthptr)
  137.                     depth = atoi(depthptr);
  138.                 else
  139.                     depth = DEFAULT_DEPTH;
  140.             }
  141.             else
  142.                 printf("Error: could not read Tool Types\n");
  143.             /* Reset current directory, if we moved it */
  144.             if(currdir)
  145.             {
  146.                 CurrentDir(currdir);
  147.                 currdir = NULL;
  148.             }
  149.             if(wbmsg->sm_NumArgs > 1)
  150.             {
  151.                 currdir = CurrentDir(wbmsg->sm_ArgList[1].wa_Lock);
  152.                 filename = wbmsg->sm_ArgList[1].wa_Name;
  153.             }
  154.         }
  155.         else
  156.         {
  157.             /* CLI bit, use ReadArgs() */
  158.             LONG args[NUM_ARGS];
  159.             int i;
  160.             /* Initialise our args to NULL */
  161.             /* (This way we will know if an argument was specified) */
  162.             for(i=0; i<NUM_ARGS; i++)
  163.                 args[i] = NULL;
  164.             if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  165.             {
  166.                 LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  167.                 portname = (char*)(args[ARG_PORTNAME]);
  168.                 filename = (char*)(args[ARG_FILENAME]);
  169.                 /* Use the default if an argument was not specified */
  170.                 if(portname == NULL)
  171.                     portname = DEFAULT_PORTNAME;
  172.                 if(depthptr == NULL)
  173.                     depth = DEFAULT_DEPTH;
  174.                 else
  175.                     depth = (UBYTE)(*depthptr);
  176.             }
  177.             else
  178.                 printf("Error: could not read arguments\n");
  179.         }
  180.         if(portname)
  181.         {
  182.             if(createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0))
  183.             {
  184.                 success = TRUE;
  185.                 if(filename)
  186.                     loadfile(filename);
  187.             }
  188.         }
  189.         /* Reset current directory, if we moved it */
  190.         if(currdir)
  191.             CurrentDir(currdir);
  192.     }
  193.     return success;
  194. }
  195.  
  196. static void freeAll()
  197. {
  198.     freeReqs();
  199.     closeGUI();
  200.     freeArgs();
  201.     freeARexxPort();
  202.     if(rdargs)
  203.         FreeArgs(rdargs);
  204.     if(dobj)
  205.         FreeDiskObject(dobj);
  206.     closeLibs();
  207. }
  208.  
  209. /* Try to open all the libraries -- return TRUE on success */
  210. static int openLibs()
  211. {
  212.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  213.     {
  214.         printf("Error: could not open graphics.library\n");
  215.         return FALSE;
  216.     }
  217.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  218.     {
  219.         printf("Error: could not open intuition.library\n");
  220.         return FALSE;
  221.     }
  222.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  223.     {
  224.         printf("Error: could not open gadtools.library\n");
  225.         return FALSE;
  226.     }
  227.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  228.     {
  229.         printf("Error: could not open asl.library\n");
  230.         return FALSE;
  231.     }
  232.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  233.     {
  234.         printf("Error: could not open dos.library\n");
  235.         return FALSE;
  236.     }
  237.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  238.     {
  239.         printf("Error: could not open iff.library\n");
  240.         return FALSE;
  241.     }
  242.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  243.     {
  244.         printf("Error: could not open rexxsyslib.library\n");
  245.         return FALSE;
  246.     }
  247.     if((IconBase = OpenLibrary("icon.library",37)) == NULL)
  248.     {
  249.         printf("Error: could not open icon.library\n");
  250.         return FALSE;
  251.     }
  252.     if(IntuitionBase->lib_Version >= 39 && GfxBase->lib_Version >= 39)
  253.     {
  254.         /* If the DataTypes library cannot be opened we fall back to using IFF */ 
  255.         if((DataTypesBase = OpenLibrary("datatypes.library", 39)) == NULL)
  256.             printf("Warning: could not open datatypes.library\n");
  257.     }
  258.   return TRUE;
  259. }
  260.  
  261. /* Close any open library */
  262. static void closeLibs()
  263. {
  264.     if(DataTypesBase)
  265.         CloseLibrary(DataTypesBase);
  266.     if(IconBase)
  267.         CloseLibrary(IconBase);
  268.     if(RexxSysBase)
  269.         CloseLibrary(RexxSysBase);
  270.     if(IFFBase)
  271.         CloseLibrary(IFFBase);
  272.     if(DosBase)
  273.         CloseLibrary(DosBase);
  274.     if(AslBase)
  275.         CloseLibrary(AslBase);
  276.     if(GadToolsBase)
  277.         CloseLibrary(GadToolsBase);
  278.     if(IntuitionBase)
  279.         CloseLibrary(IntuitionBase);
  280.     if(GfxBase)
  281.         CloseLibrary(GfxBase);
  282. }
  283.